#include  "task.h"

uint8_t debug_info[30];
bool mutex = false;

int8_t servos_dir;		//
int8_t servos_stop_index;
uint8_t wind_level;

/**
 * 手势xin获取任务，获取手势并处理
 */
void getsture_handler_task(void)
{
	if(exit_flag != 1)return;
		exit_flag = 0;
	if(isGestureAvailable()) //判断手势是否是有效的
	{
		switch (readGesture())//手势读取
		{
			case DIR_DOWN://状态：向下
				if(--wind_level>3)wind_level=0;
				printf("\r\nDIR_DOWN\r\n");
			break;
			case DIR_UP://状态：向上
				if(++wind_level>3)wind_level=0;
				printf("\r\nDIR_UP\r\n");
			break;
			case DIR_RIGHT://状态：向右,从右边开始旋转，
				servos_dir = pos_dir;
				if(++servos_stop_index>2)servos_stop_index=2;
				printf("\r\nDIR_RIGHT\r\n");
			break;
			case DIR_LEFT://状态：向左
				servos_dir = neg_dir;
				if(--servos_stop_index<-2)servos_stop_index=-2;
				printf("\r\nDIR_LEFT\r\n");
			break;
			default://状态：无
				printf("\r\nNONE\r\n");
			break;
		}
		printf("wind_level:%d,morot_dir:%d\r\n",wind_level,servos_dir);
	}
	else
		mutex=false;
}

/**
 * 风扇控制任务，控制风扇转速
 */
void motor_handler_task(void)
{
	switch(wind_level)
	{
		case 0:
			set_motor_duty(wind_stop);
			servos_dir = stop_dir;
		break;
		case 1:
			set_motor_duty(wind_level_1);
		break;
		case 2:
			set_motor_duty(wind_level_2);
		break;
		case 3:
			set_motor_duty(wind_level_3);
		break;
	}
}

/**
 * 舵机控制任务，控制风扇转向
 */
void servos_handler_task(void)
{
	static  int16_t angle = 0;

	servos_dir = (servos_stop_index==2 || servos_stop_index==-2 )? stop_dir : servos_dir;		//连续往一个方向回收表示停止
	servos_stop_index =( servos_stop_index==2 || servos_stop_index==-2 )? 0 : servos_stop_index;

	angle+=servos_dir;
	if(angle>180){angle = 180;servos_dir = -servos_dir;}
	if(angle<0){angle = 0;servos_dir = -servos_dir;}
	set_servos_angle(angle-90);
}

/**
 * 日志打印任务
 */
void printf_info_task(void)
{
	if (mutex == false)
	{
		wireReadDataByte(APDS9960_ENABLE, &debug_info[0]);Delay_Ms(10);
		wireReadDataByte(APDS9960_PDATA, &debug_info[1]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GPENTH, &debug_info[2]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GEXTH, &debug_info[3]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GCONF1, &debug_info[4]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GCONF2, &debug_info[5]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GCONF3, &debug_info[6]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GCONF4, &debug_info[7]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GFLVL, &debug_info[8]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GSTATUS, &debug_info[9]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GFIFO_U, &debug_info[10]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GFIFO_D, &debug_info[11]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GFIFO_L, &debug_info[12]);Delay_Ms(10);
		wireReadDataByte(APDS9960_GFIFO_R, &debug_info[13]);Delay_Ms(10);

		printf("\r\nINFO:APDS9960_ENABLE[0x80]:%x\r\n",debug_info[0]);
		printf("INFO:APDS9960_PDATA[0x9C]:%x\r\n",debug_info[1]);
		printf("INFO:APDS9960_GPENTH[0xA0]:%x\r\n",debug_info[2]);
		printf("INFO:APDS9960_GEXTH[0xA1]:%x\r\n",debug_info[3]);
		printf("INFO:APDS9960_GCONF1[0xA2]:%x\r\n",debug_info[4]);
		printf("INFO:APDS9960_GCONF2[0xA3]:%x\r\n",debug_info[5]);
		printf("INFO:APDS9960_GCONF3[0xAA]:%x\r\n",debug_info[6]);
		printf("INFO:APDS9960_GCONF4[0xAB]:%x\r\n",debug_info[7]);
		printf("INFO:APDS9960_GFLVL[0xAE]:%x\r\n",debug_info[8]);
		printf("INFO:APDS9960_GSTATUS[0xAF]:%x\r\n",debug_info[9]);
		printf("INFO:APDS9960_GFIFO_U[0xFC]:%x\r\n",debug_info[10]);
		printf("INFO:APDS9960_GFIFO_D[0xFD]:%x\r\n",debug_info[11]);
		printf("INFO:APDS9960_GFIFO_L[0xFE]:%x\r\n",debug_info[12]);
		printf("INFO:APDS9960_GFIFO_R[0xFF]:%x\r\n",debug_info[13]);

		printf("INFO:TIM2->ATRLR:%d,TIM2->CH1CVR:%d\r\n",TIM2->ATRLR,TIM2->CH1CVR);
		printf("INFO:TIM3->ATRLR:%d,TIM3->CH1CVR:%d\r\n",TIM3->ATRLR,TIM3->CH1CVR);
		mutex = true;
	}

}